package horseParkMonitor.configuration;


import myFiler.MyFiler;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by PROGRAMMER II on 19.12.13.
 */

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement( name ="horseList" )
public class HorseList {

 @XmlElement( name = "horse", type =Horse.class )
 private List<Horse> horseList =new ArrayList<Horse>();
 public HorseList(){}

 public HorseList(List<Horse> horseList){
     this.horseList = horseList;
 }

    public List<Horse> getHorseList() {
        return horseList;
    }

    public void setHorseList(List<Horse> horseList) {
        this.horseList = horseList;
    }

    public static final String configurationFileName= MyFiler.getCurrentDirectory()+File.separator+"configuration"+File.separator+
            "horses_info.xml";

    // Export
    public static void marshal(List<Horse> ids, File selectedFile)
            throws IOException, JAXBException {
        JAXBContext context;
        BufferedWriter writer = null;
        writer = new BufferedWriter(new FileWriter(selectedFile));
        context = JAXBContext.newInstance(HorseList.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(new HorseList(ids), writer);
        writer.close();
    }

    // Import
    public static List<Horse> unmarshal(File importFile) throws JAXBException {
        HorseList ids = new HorseList();

        JAXBContext context = JAXBContext.newInstance(HorseList.class);
        Unmarshaller um = context.createUnmarshaller();
        ids = (HorseList) um.unmarshal(importFile);

        return ids.getHorseList();
    }
    public static String getConfigurationFileName() {
        return configurationFileName;
    }


}
